﻿-- (c) 2014, all rights reserved.
-- $Revision: 1124 $
-- $Date: 2014-03-18 23:58:24 +1100 (Tue, 18 Mar 2014) $

local _G = _G
local select = _G.select
local string = _G.string
local type = _G.type
local error = _G.error

ArkInventoryRules_TSMGroup = LibStub( "AceAddon-3.0" ):NewAddon( "ArkInventoryRules_TSMGroup" )

--[[
	
	tsmgroup( ) = in any group
	tsmgroup( "test" ) = is in a group named test
	tsmgroup( "test1", "test2" ) = is in a group named either test1 or test2
	tsmgroup( "test->*" ) = is in a group named test or any of its subgroups
	tsmgroup( "test->sub1" ) = is in a group named test->sub1

]]--

local fn = "tsmgroup"
local sep = "->"
local private = { }

function private.OnEvent( event, arg )
	
	--ArkInventory.Output( "OnEvent2: ", event, " = ", arg )
	
	if event == "TSM:GROUPS:ADDITEMS" and arg and arg.isImport and arg.num == 0 then
		-- import but nothing was imported so dont reset the cache
		return
	end
	
	ArkInventoryRules.ItemCacheClear( )
	
end

function private.Execute( ... )

	-- always check for a hyperlink and that it's an item
	if not ArkInventoryRules.Object.h or ArkInventoryRules.Object.class ~= "item" then
		return false
	end
	
	if not IsAddOnLoaded( "TradeSkillMaster" ) then
		return false
	end
	
	-- full item string
	local itemString = TSMAPI:GetItemString( ArkInventoryRules.Object.h )
	
	if not itemString then
		return false
	end
	
	local group = TSMAPI:FormatGroupPath( TSMAPI:GetGroupPath( itemString ) )
	
	if not group then
		
		-- full item was not in any group, check base item
		itemString = TSMAPI:GetBaseItemString( ArkInventoryRules.Object.h )
		
		if not itemString then
			return false
		end
		
		group = TSMAPI:FormatGroupPath( TSMAPI:GetGroupPath( itemString ) )
		
		if not group then
			return false
		end
		
	end
	
	local ac = select( '#', ... )
	
	if ( ac == 0 ) then
		-- no groupnames listed, so any group is ok
		return true
	end
	
	local arg
	
	-- loop through listed groupnames
	for ax = 1, ac do
		
		arg = select( ax, ... )
		
		if not arg then
			error( string.format( ArkInventory.Localise["RULE_FAILED_ARGUMENT_IS_NIL"], fn, ax ), 0 )
		end
		
		if type( arg ) ~= "string" then
			error( string.format( ArkInventory.Localise["RULE_FAILED_ARGUMENT_IS_NOT"], fn, ax, ArkInventory.Localise["STRING"] ), 0 )
		end
		
		arg = string.trim( arg )
		
		if string.sub( arg, -1 ) == "*" then
			
			-- wildcard match, remove the wildcard
			arg = string.sub( arg, 1, -2 )
			
			if string.len( arg ) == 0 then
				-- match anything
				return true
			end
			
			-- if arg is group->* then specifically check parent group
			if string.sub( arg, -2 ) == "->" and string.lower( string.sub( arg, 1, -3 ) ) == string.lower( group ) then
				return true
			end
			
			-- check for match
			if string.lower( arg ) == string.lower( string.sub( group, 1, string.len( arg ) ) ) then
				return true
			end
			
		else
			
			-- exact match
			
			if string.len( arg ) == 0 then
				error( string.format( ArkInventory.Localise["RULE_FAILED_ARGUMENT_IS_INVALID"], fn, ax ), 0 )
			else
				if string.lower( arg ) == string.lower( group ) then
					return true
				end
			end
			
		end
		
	end
	
	return false
	
end

function ArkInventoryRules_TSMGroup:OnInitialize( )
	
	ArkInventoryRules.Register( self, fn, private.Execute )
	
	if IsAddOnLoaded( "TradeSkillMaster" ) then
		
		-- watch for tsm group events
		local eventObj = TSMAPI:GetEventObject( )
		eventObj:SetCallback( "TSM:GROUPS", private.OnEvent, true )
		
	else
		
		ArkInventory.OutputWarning( "The TradeSkillMaster addon is not loaded, the tsmgroup rule function will only return false" )
		
	end
	
end
